const fs = require('fs')
const path = require('path')
console.log();

const depVersion = '16.0.4'
const dependencies = ['spread-excelio', 'spread-sheets', 'spread-sheets-designer']
const cracks = ['spread.excelio', 'spread.sheets.all', 'spread.sheets.designer.all']

console.log('检查是否需要破解 spread-sheet')
let needCrack = false
for (const d of dependencies) {
  if (!fs.existsSync(path.join(__dirname, '../', 'node_modules', '@grapecity', d, 'dist', '.crack'))) {
    needCrack = true
  }
}

if (needCrack) {
  console.log('开始破解 spread-sheet')

  for (const [i, d] of dependencies.entries()) {
    const sourceFilePath = path.join( __dirname, `gc.${cracks[i]}.${depVersion}.min.js` )
    const targetFilePath = path.join( __dirname, '../', 'node_modules', '@grapecity', d, 'dist', `gc.${cracks[i]}.min.js` )

    fs.writeFile(path.join( __dirname, '../', 'node_modules', '@grapecity', d, 'dist', `.crack` ), '', (err) => {});

    const readStream = fs.createReadStream(sourceFilePath)
    const writeStream = fs.createWriteStream(targetFilePath)

    readStream.pipe(writeStream)

    writeStream.on('finish', () => {
      console.log(`${d} 破解成功`)
    })
  }
} else {
  console.log('无需破解')
}
